<template>
    <div class="login-container">
        <div class="login-box">
            <div style="border-radius: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); padding: 40px; background-color: white; margin-left: 100px;">
                <el-form ref="formRef" :rules="rules" :model="form" style="width: 400px;">
                    <div style="text-align: center; margin-bottom: 30px; font-size: 24px;  color: rgb(0, 46, 166); font-weight: bold;">请登录后台管理系统</div>
                    <el-form-item prop="username">
                        <el-input size="large" v-model="form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password size="large" v-model="form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>    
                    </el-form-item>
                    <el-form-item prop="role">
                        <el-select v-model="form.role" style="width: 100%;" size="large">
                            <el-option value="EMP" label="员工"></el-option>
                            <el-option value="ADMIN" label="管理员"></el-option>
                        </el-select>    
                    </el-form-item>
                    <div style="margin-bottom: 20px;">
                        <el-button @click="doLogin" size="large" style="width: 100%;" type="primary">登录</el-button>
                    </div>
                    <div>还没有账号？请<a style="text-decoration: none; color: rgb(0, 46, 166);" href="/register">注册</a></div>
                </el-form>              
            </div>

        </div>
    </div>

</template>
<script setup>
import { useUserStore } from '@/store/userStore';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { loginApi } from '@/api/user';
import { ElMessage } from "element-plus"

const router = useRouter()
const userStore = useUserStore()
const formRef = ref()
const form = ref({
    username: '',
    password: '',
    role: 'ADMIN'
})
const rules = {
    username: [
        {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
        },
        {
            min: 3,
            max: 14,
            message: '密码长度为3-14个字符或数字',
            trigger: 'blur'
        }
    ]
}
const doLogin = () => {
    formRef.value.validate(async (valid) => {
        if(valid) {
            await userStore.getUserInfo(form.value)
            if(userStore.userInfo!=null) {
                // console.log(userStore.userInfo);
                ElMessage.success("登录成功！")
                router.replace({path: '/manager/home'})
            } else {
                ElMessage.error("请重新检查身份或者账号密码！")
            }
            
        } 
    })
}
</script>
<style scoped>
.login-container{
    height: 100vh;
    overflow: hidden;
    background-image: url("@/assets/bg.jpg");
    background-size: cover;
}
.login-box{
    width: 50%;
    height: 100%;;
    position: absolute;
    /* top: 50px; */
    display: flex;
    align-items: center;
    right: 0;
}
</style>